<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Drag Element on a Line</title>
    <style>
      .line {
        position: relative;
        width: 100%;
        height: 2px;
        background-color: #000;
        margin: 50px 0;
      }

      .draggable {
        position: absolute;
        top: -10px;
        left: 0;
        width: 20px;
        height: 20px;
        background-color: red;
        border-radius: 50%;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <div class="line">
      <div class="draggable" id="draggable"></div>
    </div>

    <script>
      const draggable = document.getElementById("draggable");
      let isDragging = false;
      let startX = 0;
      let startLeft = 0;

      draggable.addEventListener("mousedown", (e) => {
        isDragging = true;
        startX = e.clientX;
        startLeft = draggable.offsetLeft;
      });

      document.addEventListener("mousemove", (e) => {
        if (isDragging) {
          const dx = e.clientX - startX;
          let newLeft =
            startLeft + dx < 0
              ? 0
              : startLeft + dx > 200
              ? 200
              : startLeft + dx;
          draggable.style.left = `${newLeft}px`;
        }
      });

      document.addEventListener("mouseup", () => {
        isDragging = false;
      });
    </script>
  </body>
</html>
